﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZhaiBB.Common;

namespace ZhaiBB.Web
{
    public partial class search : ZhaiBB.Web.UI.BasePage
    {
        public string keyword; //关健字
        public int pcount = 0; //总条数
        public int page; //当前页
        public int pagesize = 20; //设置每页显示的大小

        protected void Page_Load(object sender, EventArgs e)
        {
            keyword = Request.QueryString["keyword"];
            if (keyword != null)
            {
                keyword = keyword.Trim();
                keyword = keyword.Replace("'", "");
            }
            //取得当前页
            if (!int.TryParse(Request.Params["page"] as string, out page))
            {
                page = 0;
            }
        }

        /// <summary>
        /// 绑定查询的Repeater控件数据
        /// </summary>
        protected void Search_List_DataBind(object sender, EventArgs e)
        {
            ZhaiBB.Web.UI.Repeater _rpt = sender as ZhaiBB.Web.UI.Repeater;
            if (_rpt == null || string.IsNullOrEmpty(keyword))
                return;
            //组合查询条件
            string strWhere = "(Title like '%" + this.keyword + "%' or Content like '%" + this.keyword + "%')";
            //创建一个DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("Title", Type.GetType("System.String"));
            dt.Columns.Add("Remark", Type.GetType("System.String"));
            dt.Columns.Add("SiteUrl", Type.GetType("System.String"));
            dt.Columns.Add("AddTime", Type.GetType("System.String"));

            //查找文章模块
            ZhaiBB.BLL.Article abll = new ZhaiBB.BLL.Article();
            DataSet ads = abll.GetList(strWhere);
            if (ads.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ads.Tables[0].Rows.Count; i++)
                {
                    DataRow adr = ads.Tables[0].Rows[i];
                    DataRow dr = dt.NewRow();
                    dr["Title"] = adr["Title"]; //标题
                    dr["Remark"] = Utils.DropHTML(adr["Content"].ToString(), 250); //简介
                    dr["SiteUrl"] = URLRewrite("article_show", adr["Id"]); //链接地址
                    dr["AddTime"] = adr["AddTime"]; //发布时间
                    dt.Rows.Add(dr);
                }
            }

            //查找图文模块
            ZhaiBB.BLL.Pictures pbll = new ZhaiBB.BLL.Pictures();
            DataSet pds = pbll.GetList(strWhere);
            if (pds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < pds.Tables[0].Rows.Count; i++)
                {
                    DataRow pdr = pds.Tables[0].Rows[i];
                    DataRow dr = dt.NewRow();
                    dr["Title"] = pdr["Title"]; //标题
                    dr["Remark"] = Utils.DropHTML(pdr["Content"].ToString(), 250); //简介
                    dr["SiteUrl"] = URLRewrite("picture_show", pdr["Id"]); //链接地址
                    dr["AddTime"] = pdr["AddTime"]; //发布时间
                    dt.Rows.Add(dr);
                }
            }

            //查找下载模块
            ZhaiBB.BLL.Downloads dbll = new ZhaiBB.BLL.Downloads();
            DataSet dds = dbll.GetList(strWhere);
            if (dds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dds.Tables[0].Rows.Count; i++)
                {
                    DataRow ddr = dds.Tables[0].Rows[i];
                    DataRow dr = dt.NewRow();
                    dr["Title"] = ddr["Title"]; //标题
                    dr["Remark"] = Utils.DropHTML(ddr["Content"].ToString(), 250); //简介
                    dr["SiteUrl"] = URLRewrite("download_show", ddr["Id"]); //链接地址
                    dr["AddTime"] = ddr["AddTime"]; //发布时间
                    dt.Rows.Add(dr);
                }
            }

            //取得数据总数
            if (dt.Rows.Count > 0)
            {
                this.pcount = dt.Rows.Count;
            }
            //排序
            DataRow[] _rowsArr = dt.Select("", "AddTime desc");
            DataTable _dt = dt.Clone();
            _dt.Clear();
            foreach (DataRow row in _rowsArr)
            {
                _dt.ImportRow(row);
            }
            dt = _dt;

            //利用PAGEDDAGASOURCE类来分页
            PagedDataSource pg = new PagedDataSource();
            pg.DataSource = dt.DefaultView;
            pg.AllowPaging = true;
            pg.PageSize = this.pagesize;
            pg.CurrentPageIndex = this.page;

            //绑定数据
            _rpt.DataSource = pg;
            _rpt.DataBind();
        }
    }
}
